<?php
App::uses('AppController', 'Controller');
/**
 * Planespagos Controller
 *
 * @property Planespago $Planespago
 */
class PlanespagosController extends AppController {

    public $layout = 'colegio';
    public $helpers = array('Utilidades');
    //public $components = array('Fechasconvert');
    public $uses =array('Pago', 'Planespago');

    public function beforeFilter()
    {
        parent::beforeFilter();
        //$this->Auth->allow('*');
    }
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$planespagos = $this->Planespago->find('all', array('recursive'=>0));
		$this->set(compact('planespagos'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Planespago->id = $id;
		if (!$this->Planespago->exists()) {
			throw new NotFoundException(__('Plan de pago inv&aacute;lido'));
		}
        $planespago = $this->Planespago->read(null, $id);
		$this->set(compact('planespago', 'id'));
	}
/**
 * ver pagos del plan method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function verpagos($id = null) {
		$this->Planespago->id = $id;
		if (!$this->Planespago->exists()) {
			throw new NotFoundException(__('Plan de pago inv&aacute;lido'));
		}
        //debug($this->Planespago->read(null, $id));exit;
		$this->set('planespago', $this->Planespago->read(null, $id));
	}


/**
 * add method
 *
 * @return void
 */
	public function add() {
		if (!empty($this->request->data)) {
			$this->Planespago->create();
			if ($this->Planespago->save($this->request->data)) {
				$this->Session->setFlash(__('El nuevo plan de pago fue registrado'), 'msgbueno');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El plan de pago no pudo guardarse. Por favor, intente nuevamente.'));
			}
		}
	}
	public function ajaxadd() {
	   $this->layout = 'ajax';
		if (!empty($this->request->data)) {
			$this->Planespago->create();
			if ($this->Planespago->save($this->request->data)) {
				$this->Session->setFlash(__('El nuevo plan de pago fue registrado'), 'msgbueno');
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El plan de pago no pudo guardarse. Por favor, intente nuevamente.'));
			}
		}
	}
/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Planespago->id = $id;
		if (!$this->Planespago->exists()) {
			throw new NotFoundException(__('Plan invalido'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Planespago->save($this->request->data)) {
				$this->Session->setFlash(__('El pago fue editado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El plan no pudo guardarse. Por favor, intente nuevamente.'));
			}
		} else {
			$this->request->data = $this->Planespago->read(null, $id);
		}
	}
public function ajaxedit($id = null) {
    $this->layout='ajax';
		$this->Planespago->id = $id;
		if (!$this->Planespago->exists()) {
			throw new NotFoundException(__('Plan invalido'));
            $this->redirect(array('action' => 'index'));
		}
		if (!empty($this->request->data)) {
			if ($this->Planespago->save($this->request->data)) {
				$this->Session->setFlash(__('El pago fue editado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('El plan no pudo guardarse. Por favor, intente nuevamente.'));
			}
		} else {
			$this->request->data = $this->Planespago->read(null, $id);
		}
	}
/**
 * delete method
 *
 * @throws MethodNotAllowedException
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		$this->Planespago->id = $id;
		if (!$this->Planespago->exists()) {
			throw new NotFoundException(__('Invalid planespago'));
		}
        $pagos=$this->Pago->find('count',array(
                'conditions'=>array('Pago.planespago_id'=>$id)
        ));
        
        if($pagos!=0){
            $this->Session->setFlash(__('No se puede eliminar el Plan porque ya existen pagos asociados a este plan...'), 'msgalert');
			$this->redirect(array('action' => 'index'));
        }
		if ($this->Planespago->delete()) {
			$this->Session->setFlash(__('Planespago deleted'));
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Planespago was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
    public function eliminar($id = null) {
		$this->Planespago->id = $id;
		if (!$this->Planespago->exists()) {
			throw new NotFoundException(__('Invalid planespago'));
		}
		if ($this->Planespago->delete()) {
			$this->Session->setFlash(__('Plane de pago eliminado'), 'msgbueno');
			$this->redirect(array('action' => 'index'));
		}
		$this->Session->setFlash(__('Planespago was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
}
